﻿using System.IO;
using UnityEditor;
using UnityEngine;

public class MapToJson 
{
    static string SceneObjPath = "/Resources/Prefab/SceneObjs";
    static string SceneJsonPath = "/Resources/Json/SceneObjs.json";
    static string tempStr = "  {\n\t\"ID\":{0},\n\t\"path\":\"{1}\"\n  }";

    [MenuItem("Tools/SceneObjToJson")]
    private static void SceneObjsToJson()
    {
        string fullPath = Application.dataPath + SceneObjPath;
        string[] dirs = Directory.GetFiles(fullPath, "*.prefab", SearchOption.AllDirectories);
        fullPath = Application.dataPath + "/Resources/";

        string temp = ""; string jsonStr = "[\n";
        for (int i = 1; i <= dirs.Length; i++)
        {
            string goPath = dirs[i - 1].Replace(fullPath, "").Replace(".prefab", "").Replace("\\", "/");
            EditorUtility.DisplayProgressBar("处理中 请稍等", "正在处理：" + goPath, (float)i / dirs.Length);
            temp = string.Format(tempStr, i, goPath);
            if (i < dirs.Length)
            {
                temp += ",\n";
            }
            jsonStr += temp;
        }
        jsonStr += "\n]"; string path = Application.dataPath + SceneJsonPath;
        using (var file = File.CreateText(path))
        {
            file.WriteLine(jsonStr);
        }
        EditorUtility.ClearProgressBar(); AssetDatabase.Refresh();
        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);
    }
}